<template>
  <div class="tablecontain">
    <el-table :data="tableData" height="90%" :border="true" :stripe="true" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="200"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="200"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="200">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.sex == 1 ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="birth" label="出生日期" width="200"> </el-table-column>
      <el-table-column prop="addr" label="地址" width="300"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="medium" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="medium" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <PaGination></PaGination>
  </div>
</template>

<script>
import PaGination from './PaGination.vue'
import { mapState, mapMutations } from 'vuex'
import { getUser, deleteUser } from '@/api/index.js'
export default {
  mounted() {
    getUser().then(({ data }) => {
      // this.tableData = data.list
      this.gettableData(data)
    })
  },
  data() {
    return {
      // tableData: []
    }
  },
  components: {
    PaGination
  },
  methods: {
    ...mapMutations(['edittable', 'gettableData']),
    handleEdit(row) {
      // console.log(index, row)
      this.edittable(row)
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(() => {
          deleteUser({ id: row.id }).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            getUser().then(({ data }) => {
              this.gettableData(data)
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  computed: {
    ...mapState({
      modelType: state => state.tab.modelType,
      tableData: state => state.tab.tableList
    })
  }
}
</script>

<style lang="less" scoped>
.tablecontain {
  height: 700px;
}
</style>
